package com.ruoyi.finance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.finance.domain.PmsCategory;
import com.ruoyi.finance.mapper.CategoryMapper;
import com.ruoyi.finance.service.CategoryService;
import com.ruoyi.finance.vo.CatalogTreeVo;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: qs
 * @Date: 2025/3/8 10:08
 * @Description:
 **/
@Service
public class CategoryServicelmpl extends ServiceImpl<CategoryMapper, PmsCategory> implements CategoryService {

    @Override
    public List<Tree<String>> listwithTree() {
        List<PmsCategory> categoryEntities = baseMapper.selectList(Wrappers.emptyWrapper());
        List<TreeNode<String>> catLevel = categoryEntities.stream().map(categoryEntity -> {
            TreeNode<String> stringTreeNode = new TreeNode<>();
            //通过setId方法将数据库中的分类 ID（catId）转换为字符串后设置为树节点的 ID
            stringTreeNode.setId(categoryEntity.getCatId().toString());
            //通过setParentId方法将数据库中的父分类 ID（parentCid）转换为字符串后设置为树节点的父 ID。
            stringTreeNode.setParentId(categoryEntity.getParentCid().toString());
            //通过setName方法将分类名称（name）设置为树节点的名称。
            stringTreeNode.setName(categoryEntity.getName());
            //通过setWeight方法将排序字段（sort）设置为树节点的权重。
            stringTreeNode.setWeight(categoryEntity.getSort());
            //创建一个HashMap，将分类层级（catLevel）作为键值对存入，然后通过setExtra方法将这个HashMap设置为树节点的额外属性。
            HashMap<String, Object> map = new HashMap<>();
            map.put("catLevel", categoryEntity.getCatLevel());
            stringTreeNode.setExtra(map);
            return stringTreeNode;
        }).collect(Collectors.toList());
        List<Tree<String>> build = TreeUtil.build(catLevel, "0");
        return build;
    }

    @Override
    public List<PmsCategory> keeptree() {
        List<PmsCategory> pmsCategories = baseMapper.selectList(Wrappers.emptyWrapper());
        //查询出所有一级菜单
        List<PmsCategory> collect = pmsCategories.stream()
                .filter(pmsCategory -> pmsCategory.getParentCid() == 0)
                .map(pmsCategory -> {
                    //往查询出来的当前对象里存入子菜单(chirdent)
                  pmsCategory.setPmsCategories(instertree(pmsCategory, pmsCategories));
                  return pmsCategory;
                })
                .collect(Collectors.toList());
        return collect;

    }

    private List<PmsCategory> instertree(PmsCategory root,List<PmsCategory> all) {
        List<PmsCategory> collect = all.stream().filter(category -> {
            return category.getParentCid() == root.getCatId();
        }).map(category -> {
            category.setPmsCategories(instertree(category, all));
            return category;
        }).collect(Collectors.toList());
        return collect;
    }


}
